package com.czjtxy.extraTerm.daoImpl;

import com.czjtxy.extraTerm.dao.TuserDao;
import com.czjtxy.extraTerm.entity.DO.Tuser;
import com.czjtxy.extraTerm.util.Util;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: staff-house
 * @author: MingYi
 * @create: 2021/06/30 08:37
 */
public class TuserDaoImpl extends BaseDaoImpl implements TuserDao {
    @Override
    public Tuser login(String loginName, String pwd) throws ClassNotFoundException, SQLException {
        super.openDB();
        //sql语句
        String sql = "SELECT * FROM user_inf WHERE loginname = ? and password = ?";
        //预编译
        ps = con.prepareStatement(sql);
        //处理占位符
        ps.setString(1, loginName);
        ps.setString(2, pwd);
        //执行SQL
        rs = ps.executeQuery();
        Tuser tuser = null;
        if (rs.next()) {
            tuser = conventRsToTuser(rs);
        }
        return tuser;
    }

    @Override
    public List<Tuser> getAll() throws ClassNotFoundException, SQLException {
        super.openDB();
        String sql = "SELECT * FROM user_inf";
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();
        List<Tuser> tusers = new ArrayList<>();
        while (rs.next()) {
            tusers.add(conventRsToTuser(rs));
        }
        return tusers;
    }

    @Override
    public List<Tuser> getPart(Tuser tuser) throws ClassNotFoundException, SQLException {
        super.openDB();
        String sql = "SELECT * FROM user_inf WHERE username LIKE ? AND STATUS LIKE ?";
        ps = con.prepareStatement(sql);
        ps.setString(1, Util.likeStr(tuser.getUserName()));
        if (tuser.getStatus() == 0) {
            ps.setString(2, "%");
        } else {
            ps.setInt(2, tuser.getStatus());
        }
        rs = ps.executeQuery();
        List<Tuser> tusers = new ArrayList<>();
        while (rs.next()) {
            tusers.add(conventRsToTuser(rs));
        }
        return tusers;
    }

    @Override
    public Tuser getOne(int uid) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "SELECT * FROM user_inf WHERE ID = ?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, uid);
        rs = ps.executeQuery();
        Tuser tuser = null;
        if (rs.next()) {
            tuser = conventRsToTuser(rs);
        }
        return tuser;
    }

    @Override
    public void updateUser(Tuser tuser) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "UPDATE user_inf SET loginname = ? , username = ? , STATUS = ? WHERE ID = ?";
        ps = conventTuserToPs(sql, tuser);
        ps.setInt(4, tuser.getUid());
        ps.executeUpdate();
    }

    @Override
    public void deleteUser(int uid) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "DELETE FROM user_inf WHERE ID = ?";
        ps = con.prepareStatement(sql);
        ps.setInt(1, uid);
        ps.executeUpdate();
    }

    @Override
    public void addUser(Tuser tuser) throws SQLException, ClassNotFoundException {
        super.openDB();
        String sql = "INSERT INTO user_inf (loginname, username, STATUS, createdate, PASSWORD) VALUES (?,?,?,NOW(),?)";
        ps = conventTuserToPs(sql, tuser);
        ps.setString(4, tuser.getPassword());
        ps.executeUpdate();
    }

    private PreparedStatement conventTuserToPs(String sql, Tuser tuser) throws SQLException {
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, tuser.getLoginName());
        ps.setString(2, tuser.getUserName());
        ps.setInt(3, tuser.getStatus());
        return ps;
    }

    private Tuser conventRsToTuser(ResultSet rs) throws SQLException {
        Tuser tuser = new Tuser();
        tuser.setCreateDate(rs.getDate("createdate"));
        tuser.setFacePath(rs.getString("facepath"));
        tuser.setFaceUrl(rs.getString("faceurl"));
        tuser.setLoginName(rs.getString("loginname"));
        tuser.setPassword(rs.getString("password"));
        tuser.setStatus(rs.getInt("status"));
        tuser.setUid(rs.getInt("id"));
        tuser.setUserName(rs.getString("username"));
        return tuser;
    }
}
